﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wicresoft.UnifyShow.Tool.Init;
using Wicresoft.UnifyShow.Tool.Model;
using Wicresoft.UnifyShow.Tool.Service;

namespace Wicresoft.UnifyShow.Tool.CreateUser
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("To create user info to database! \n Please click Enter key!");
            Console.ReadLine();
            Console.WriteLine("Starting...\n");

            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\\DataPool\\Data.xls;Extended Properties=Excel 8.0 ";

            OleDbConnection myConn = new OleDbConnection(strCon);
            myConn.Open();
            string[] Sheet = new string[] { "LoginIDIsNull" };
            for (int n = 0; n < Sheet.Length; n++)
            {
                DataSet myDataSet = new DataSet();
                string StrSql = "select * from [" + Sheet[n] + "$]";
                OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
                myCommand.Fill(myDataSet, "[" + Sheet[n] + "$]");
                myCommand.Dispose();
                DataTable DT = myDataSet.Tables["[" + Sheet[n] + "$]"];
                myConn.Close();
                myCommand.Dispose();

                if (Sheet[n].ToString() == "LoginIDIsNull")
                {
                    User_XModel model = new User_XModel();
                    InitApp MyInit=new InitApp();
                    int ret = 0;

                    string r0 = "";
                    string r1 = "";
                    string r2 = "";
                    string r3 = "";
                    string r4 = "";
                    string r5 = "";
                    string r6 = "";

                    for (int j = 1; j < DT.Rows.Count; j++)
                    {
                        int row = j + 2;
                        Console.WriteLine("Check the Excel data row :" + row);
                        r0 = DT.Rows[j][0].ToString().Replace(" ", "");
                        r1 = DT.Rows[j][1].ToString().Replace(" ", "");
                        r2 = DT.Rows[j][2].ToString().Replace(" ", "");
                        r3 = DT.Rows[j][3].ToString().Replace(" ", "");
                        r4 = DT.Rows[j][4].ToString().Replace(" ", "");
                        r5 = DT.Rows[j][5].ToString().Replace(" ", "");
                        r6 = DT.Rows[j][6].ToString().Replace(" ", "");

                        if (!string.IsNullOrEmpty(r0)) 
                        {
                            model.Name = r0;
                            model.DisplayName = r1;
                            model.Company=r2;
                            model.Department = r3;
                            model.Position = r4;
                            model.Mobile = r5;
                            model.Email = r6;
                            model.Password = r0;
                            ret = MyInit.Create(model);
                        }

                        if (ret == 1)
                        {
                            string msg = "Exist : " + r0 + " ; Row [" + row + "]";
                            SysLog.WriteSysLog(msg);
                        }
                        else if (ret == 2) 
                        {
                            string msg = "Error : " + r0 + " ; in row [" + row + "]";
                            SysLog.WriteSysLog(msg);
                        }
                    }
                }

            }

            Console.WriteLine("All user info data were created!");
            Console.ReadLine();

        }
    }
}
